<template>
  <div class="app-container">
    <el-row :gutter="10" style="position: relative; margin-bottom: 5px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdate(2)"
          v-hasPermi="['repertory:WarehouseAdjustments:edit']"
        >
          批量修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['repertory:WarehouseAdjustments:import']"
        >
          批量导入
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      border
      highlight-current-row
      :height="tableH"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      :data="materielList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column fixed type="selection" align="center" />
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        align="center"
        prop="itmWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        prop="itmPmgg"
      />
      <el-table-column label="第一仓库" align="center" prop="itmCkdm">
        <template #default="scope">
          <dict-tag
            :options="storageLists"
            :value="scope.row.itmCkdm"
            label-key="storageName"
            value-key="storageId"
          />
        </template>
      </el-table-column>
      <el-table-column label="库位" align="center" prop="itmCwdmName" />
      <el-table-column
        label="第二仓库"
        :show-overflow-tooltip="true"
        align="center"
        prop="itmCkdmTwo"
      >
        <template #default="scope">
          <dict-tag
            :options="storageLists"
            :value="scope.row.itmCkdmTwo"
            label-key="storageName"
            value-key="storageId"
          />
        </template>
      </el-table-column>
      <el-table-column label="库位" align="center" prop="itmCwdmNameTwo" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(1, scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label-width="125px" label="第一仓库" prop="itmCkdm">
            <el-select
              v-model="queryParams.itmCkdm"
              style="width: 400px"
              placeholder="请输入第一仓库代码"
            >
              <el-option
                v-for="dict in storageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" label="第二仓库" prop="itmCkdmTwo">
            <el-select
              v-model="queryParams.itmCkdmTwo"
              style="width: 400px"
              placeholder="请输入第二仓库代码"
            >
              <el-option
                v-for="dict in storageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" prop="totalOrder">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.totalOrder"
              placeholder="请输入物料代码或品名规格或客户编码"
              clearable
              style="width: 400px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" label="建立日期" prop="createTime">
            <el-date-picker
              clearable
              size="small"
              v-model="queryParams.createTimes"
              :picker-options="pickerOptions"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              :clearable="false"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="display: flex; margin-top: 20px; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 修改生产物料信息对话框 -->
    <el-dialog
      :title="title"
      v-model="openStr"
      :close-on-click-modal="false"
      draggable
      width="750px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="第一仓库" prop="itmCkdm">
            <el-select v-model="form.itmCkdm" placeholder="请选择第一仓库">
              <el-option
                v-for="dict in storageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库位" prop="itmCwdmName"
            ><el-input v-model="form.itmCwdmName"></el-input
          ></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="第二仓库" prop="itmCkdmTwo">
            <el-select v-model="form.itmCkdmTwo" placeholder="请选择第二仓库">
              <el-option
                v-for="dict in storageLists"
                :key="dict.storageId"
                :label="dict.storageName"
                :value="dict.storageId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库位" prop="itmCwdmNameTwo"
            ><el-input v-model="form.itmCwdmNameTwo"></el-input
          ></el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelStr">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="upload.title"
      v-model="upload.open"
      :close-on-click-modal="false"
      width="400px"
      draggable
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!-- <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link> -->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">取 消</el-button>
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMateriel } from "@/api/project/materiel";

import { getstorageType } from "@/api/repertory/Storage";
import { updateKcmLocation } from "@/api/repertory/WarehouseAdjustments";
import { getToken } from "@/utils/auth";
export default {
  name: "WarehouseAdjustments",
  data() {
    return {
      flag: false,
      //物料Id
      itmMstr: null,
      //客户ID
      cusId: null,
      //客户分类
      cus: [],
      //大分类信息
      bigLbm: [],
      //中分类
      middleLbm: [],
      //小分类
      smallLbm: [],
      //仓位信息
      storageList: [],

      //当前物料代码
      itmData: null,

      //添加厂商对话框
      addCsmView: false,

      //所有厂商
      allCsmData: [],

      //物料厂商列表
      csmList: [],
      //仓库信息
      storageLists: [],
      //机种信息
      jzmList: [],
      //分类Form
      lbmForm: {},
      // 遮罩层
      loading: true,
      tableH: document.body.clientHeight / 2 - 82,
      tableHChild: document.body.clientHeight / 2 - 100,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],

      timer: null,
      itmImg: null,

      //选中的物料名称
      itmWldms: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产物料信息表格数据
      materielList: [],
      // 弹出层标题
      title: "",
      // 是否弹出新增对话框
      open: false,
      openSearch: false,
      //是否弹出修改对话框
      openStr: false,
      //上传文件对话框
      openFile: false,
      // 单位字典
      itmDwOptions: [],

      //所有单位字典
      itmDwAllOptions: [],
      //制造车间字典
      itCarOptions: [],
      //有效标识
      csmWldmFlagOptions: [],
      // 查询参数
      queryParams: {
        totalOrder: null,
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 14 : 20,
        itmWldm: null,
        itmPmgg: null,
        itmCustwldm: null,
        itmCkdm: null,
        itmCkdmTwo: null,
        createBy: null,
        createTimes: null,
        updateBy: null,
        updateTime: null,
        beginDate: null,
        endDate: null,
      },
      // 表单参数
      form: {
        itmCkdmName: null,
        itmCwdmName: null,
        itmCkdmTwo: null,
        itmCwdmNameTwo: null,
      },
      // 表单校验
      rules: {},

      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/repertory/kcmData/import",
      },
    };
  },
  created() {
    this.getList();

    getstorageType(0).then((response) => {
      this.storageLists = response.data.map((a) => {
        a.listClass = "default";
        a.storageId = "" + a.storageId;
        return a;
      });
    });
  },
  methods: {
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产物料信息";
      this.openSearch = true;
    },
    /** 查询生产物料信息列表 */
    getList(row) {
      this.loading = true;
      listMateriel(this.queryParams).then((response) => {
        this.materielList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    cancelStr() {
      this.openStr = false;
      this.reset();
    },
    cancelCsm() {
      this.addCsmView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itmId: null,
        itmWldm: null,
        itmPrice: null,
        itmWlpm: null,
        itmWlgg: null,
        itmPmgg: null,
        itmCzdm: null,
        itmYsdm: null,
        itmQtinf: null,
        itmMz: null,
        itmJz: 0,
        itmSkzl: 0,
        itmDw: null,
        itmCustwldm: null,
        itmTzbh: null,
        itmCgsx: null,
        itmMinbz: 0,
        itmCwdm: null,
        itmCkdm: null,
        itmKwdm: null,
        itmJzdm: null,
        itmCcms: null,
        itmZtbz: null,
        itmEdzq: 0,
        itmTedzp: null,
        itmShry: null,
        itmShrq: null,
        itmPzry: null,
        itmPzrq: null,
        itmJccc1: null,
        itmJccc2: null,
        itmJccc3: null,
        itmJczl: null,
        itmCj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        cusId: null,
        bigLbmId: null,
        middleLbmId: null,
        smallLbmId: null,
        cusCode: null,
        bigLbmCode: null,
        middleLbmCode: null,
        smallLbmCode: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.createTimes != null) {
        this.queryParams.beginDate = this.queryParams.createTimes[0];
        this.queryParams.endDate = this.queryParams.createTimes[1];
      }
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.itmId);
      this.itmWldms = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(flag, row) {
      if (flag == 1) {
        this.form = Object.assign({}, row);
        delete this.form.itmCwdmName;
        delete this.form.itmCwdmNameTwo;
        this.flag = true;
      } else {
        this.flag = false;
        this.reset();
      }

      this.title = "修改仓库";
      this.openStr = true;
    },

    /** 提交按钮 */
    submitForm() {
      if (this.flag) {
        updateKcmLocation([this.form]).then((res) => {
          this.openStr = false;
          this.msgSuccess("修改成功");
          this.getList();
        });
      } else {
        for (let o of this.itmWldms) {
          o.itmCkdm = this.form.itmCkdm;
          o.itmCkdmTwo = this.form.itmCkdmTwo;
          o.itmCwdmName = this.form.itmCwdmName;
          o.itmCwdmNameTwo = this.form.itmCwdmNameTwo;
        }
        updateKcmLocation(this.itmWldms).then((res) => {
          this.openStr = false;
          this.msgSuccess("修改成功");
          this.getList();
        });
      }
    },
    handleImport() {
      this.upload.title = "物料资产批量更新单价";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      if (response.data) {
        this.download(response.data.msg);
      }
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 下载模板操作 */
    // importTemplate() {
    //   exportMonExample().then((response) => {
    //     this.download(response.msg)
    //   })
    // },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style lang="scss" scoped>
.unit-parent {
  height: 34px;
}

.unit {
  margin-left: 10px;
  height: 34px;
  line-height: 34px;
}

.unit-button {
  margin-left: 10px;
  height: 38px;
  line-height: 38px;
}

.el-table__body tr.current-row > td {
  background-color: rgb(196, 224, 248);
  color: darkslategray;
}

.el-table--enable-row-hover .el-table__body tr:hover > td {
  background-color: rgb(196, 224, 248);
  color: darkslategray;
}

.el-table .cell {
  white-space: nowrap;
}
</style>
